package lvm

import (
    "context"
    "fmt"
    "os/exec"
    "strings"
    "volux/pkg/storage"
)

type LVMAdapter struct {
    vg     string
    config LVMConfig
}

type LVMConfig struct {
    VolumeGroup string
    DataAlign   string
    AllowZero   bool
}

func NewLVMAdapter(config LVMConfig) (*LVMAdapter, error) {
    // 验证卷组是否存在
    cmd := exec.Command("vgs", config.VolumeGroup)
    if err := cmd.Run(); err != nil {
        return nil, fmt.Errorf("volume group %s not found", config.VolumeGroup)
    }

    return &LVMAdapter{
        vg:     config.VolumeGroup,
        config: config,
    }, nil
}

func (l *LVMAdapter) Create(ctx context.Context, name string, sizeBytes uint64) error {
    sizeGB := float64(sizeBytes) / (1024 * 1024 * 1024)
    
    args := []string{
        "lvcreate",
        "-n", name,
        "-L", fmt.Sprintf("%.2fG", sizeGB),
        l.vg,
    }
    
    if l.config.DataAlign != "" {
        args = append(args, "--dataalignment", l.config.DataAlign)
    }
    
    if l.config.AllowZero {
        args = append(args, "-y")
    }
    
    cmd := exec.CommandContext(ctx, args[0], args[1:]...)
    output, err := cmd.CombinedOutput()
    if err != nil {
        return fmt.Errorf("failed to create LV: %v, output: %s", err, string(output))
    }
    
    return nil
}

// ...implement other interface methods...
